package UI;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Hashtable;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

import Debug.Debug;
import Interfaces.C_interface;
import Interfaces.S_interface;
import Structs.Log;
import Structs.Msg_broad;
import Structs.Msg_rsp;
import Structs.Task;
import Threads.S_Broadcast;
import Threads.S_GrimReaper;
import Threads.S_RMI;
import Threads.S_TCP_Starter;
import Threads.S_UDP_Alive;
import Threads.S_UDP_listener;
import Threads.S_dispatcher;
import Common.common;

public class Server_Drop extends common {
	
	private static BlockingQueue<Msg_rsp> rmi_dispatch;
	private static BlockingQueue<Msg_broad> Broadcast;
 	private static BlockingQueue<Thread> GrimReaper;
 	private static BlockingDeque<Log> log;
	private static BlockingQueue<Task> Task_Queue;
	
 	private static Hashtable Clients;
	private static Hashtable<Integer, Task> Tasks;
	private static Hashtable OnlineClients;
	
	private static int RMI_port = 7000;
	private static int TCP_port = 6000;
	private static int Files_port = 5000;
	private static int failonver_port = 4000;
	
	public static void main(String[] args)  throws RemoteException{
		
		Task_Queue = new LinkedBlockingQueue<Task>();
		GrimReaper = new LinkedBlockingQueue<Thread>();
		Broadcast = new LinkedBlockingQueue<Msg_broad>();
		rmi_dispatch = new LinkedBlockingQueue<Msg_rsp>();
		log = new LinkedBlockingDeque<Log>();
		
		Clients = new Hashtable();
		Tasks = new Hashtable();
		OnlineClients = new Hashtable();
		
		Thread Failover = new Thread(new S_UDP_listener(failonver_port));
		
		Failover.start();
		try{
			Failover.join();
		} catch (InterruptedException e){
			debug("bode!!!");
		}
		
		new S_UDP_Alive(failonver_port);
		
		S_RMI rmi = new S_RMI(RMI_port,Clients,OnlineClients,Tasks,Task_Queue,Broadcast,log);
		
		new S_TCP_Starter(TCP_port,Clients,Tasks,GrimReaper,Broadcast,log,OnlineClients);
		new S_GrimReaper(GrimReaper);
		new S_Broadcast(OnlineClients, Broadcast, log);
		
		try {
			Registry r = LocateRegistry.createRegistry(RMI_port);
			r.rebind("MyDrop", rmi);
			
			new S_dispatcher(Tasks, Broadcast, log, OnlineClients,Task_Queue);
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
